package com.it.admin.controller;

import com.it.admin.model.dto.article.*;
import com.it.admin.service.AdminArticleService;
import com.it.common.aspect.ApiOperationLog;
import com.it.common.utils.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/article")
@Api(tags = "Admin 文章模块")
public class AdminArticleController {

    @Resource
    private AdminArticleService adminArticleService;


    @PostMapping("/publish")
    @ApiOperation(value = "文章发布")
    @ApiOperationLog(description = "文章发布")
    @PreAuthorize(value = "hasRole('admin')")
    public Response publishArticle(@RequestBody @Validated PublishArticleDto publishArticleDto){

        return adminArticleService.publishArticle(publishArticleDto);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "文章删除")
    @ApiOperationLog(description = "文章删除")
    @PreAuthorize(value = "hasRole('admin')")
    public Response deleteArticle(@RequestBody @Validated DeleteArticleDto deleteArticleDto){
        return adminArticleService.deleteArticle(deleteArticleDto);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询文章分页数据")
    @ApiOperationLog(description = "查询文章分页数据")
    public Response findArticlePageList(@RequestBody @Validated FindArticlePageListDto findArticlePageListDto) {
        return adminArticleService.findArticlePageList(findArticlePageListDto);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "查询文章详情")
    @ApiOperationLog(description = "查询文章详情")
    public Response findArticleDetail(@RequestBody @Validated FindArticleDetailDto findArticleDetailDto) {
        return adminArticleService.findArticleDetail(findArticleDetailDto);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新文章")
    @ApiOperationLog(description = "更新文章")
    @PreAuthorize(value = "hasRole('admin')")
    public Response updateArticle(@RequestBody @Validated UpdateArticleDto updateArticleDto){
        return adminArticleService.updateArticle(updateArticleDto);
    }

    @PostMapping("/isTop/update")
    @ApiOperation(value = "更新文章置顶状态")
    @ApiOperationLog(description = "更新文章置顶状态")
    @PreAuthorize(value = "hasRole('admin')")
    public Response updateArticleIsTop(@RequestBody @Validated UpdateArticleIsTopDto updateArticleIsTopDto) {

        return adminArticleService.updateArticleIsTop(updateArticleIsTopDto);
    }

}
